package com.edu.test;

import com.edu.bean.Book;
import com.edu.bean.User;
import com.edu.mapper.BookMapper;
import com.edu.mapper.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import java.io.InputStream;

/**
 * @Author:mx
 * @Date:2025/10/20-17:04:11
 */
public class MyTest03 {

    SqlSession session1 = null;

    SqlSession session2 = null;

    @Before
    public void init() throws Exception{
        String path = "mybatis-config.xml";
        InputStream ins = Resources.getResourceAsStream(path);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(ins);
        session1 = sqlSessionFactory.openSession();
        session2 = sqlSessionFactory.openSession();
    }

    @Test
    public void test01(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookById(1);//存 一级缓存
        System.out.println(book);
        System.out.println("------------");
//        session1.commit();
        Book book2 = bookMapper.selectBookById(1);
        System.out.println(book2);
    }


    @Test
    public void test02(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookById(1); // 查完 存入 一级缓存
        System.out.println(book);
        System.out.println("------------");
        Book book2 = bookMapper.selectBookById(1); // 查一级缓存
        Book book3 = bookMapper2.selectBookById(1);// 不同的session 不走缓存
        session1.commit(); // session1 查的数据 存入 二级缓存
        Book book4 = bookMapper.selectBookById(1); // 走二级缓存
        Book book54 = bookMapper2.selectBookById(1);//走二级缓存
        System.out.println(book2);
    }

    //同一个 命名空间下 （同一个mapper） 执行了 增删改后 二级缓存失效
    @Test
    public void test03(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookById(1); // 查完 存入 一级缓存
        System.out.println(book);
        session1.commit();//存入二级缓存
        Book book2 = bookMapper2.selectBookById(1); //查二级缓存
        bookMapper.deleteBook(15);
        session1.commit();
        Book book3 = bookMapper2.selectBookById(1); //查二级缓存
    }

    @Test
    public void test04(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        UserMapper userMapper = session1.getMapper(UserMapper.class);
        BookMapper bookMapper2 = session2.getMapper(BookMapper.class);
        UserMapper userMapper2 = session1.getMapper(UserMapper.class);

        Book book = bookMapper.selectBookById(1); //存session1 一级缓存
        User user = userMapper.selectUserById(1);//存入session1 一级缓存
        System.out.println(book);
        session1.commit(); // book 存入 BookMapper的 二级缓存
        userMapper2.deleteUser(1);
        session2.commit();//清空 session1的 一级缓存 清除 UserMapper的 二级缓存
        Book book2 = bookMapper.selectBookById(1);//二级缓存
        User user2 = userMapper.selectUserById(1);//数据库
    }


    @Test
    public void test05(){
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        UserMapper userMapper2 = session2.getMapper(UserMapper.class);

        Book book = bookMapper.selectBookById(1); //存session1 一级缓存
        session1.commit(); // book 存入 BookMapper的 二级缓存
        userMapper2.deleteUser(1);
        session2.commit();//清空 session1的 一级缓存 清除 UserMapper的 二级缓存
        Book book2 = bookMapper.selectBookById(1);

    }

    @Test
    public void test06() throws InterruptedException {
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookById(1); //存session1 一级缓存
        session1.commit(); // book 存入 BookMapper的 二级缓存
        Book book2 = bookMapper.selectBookById(1);
        Thread.sleep(6000);
        Book book3 = bookMapper.selectBookById(1);
    }

    @Test
    public void test07() throws InterruptedException {
        BookMapper bookMapper = session1.getMapper(BookMapper.class);
        Book book = bookMapper.selectBookById(1);
        Book book2 = bookMapper.selectBookById(2);
        Book book3 = bookMapper.selectBookById(3);
        Book book4 = bookMapper.selectBookById(4);
        Book book5 = bookMapper.selectBookById(5);
        Book book6 = bookMapper.selectBookById(6);
        Book book7 = bookMapper.selectBookById(7);
        Book book8 = bookMapper.selectBookById(8);
        Book book9 = bookMapper.selectBookById(9);
        Book book10 = bookMapper.selectBookById(10);
        Book book11 = bookMapper.selectBookById(11);
        session1.commit(); // book 存入 BookMapper的 二级缓存
        System.out.println("------");
        Book book21 = bookMapper.selectBookById(1);
        Book book22 = bookMapper.selectBookById(2);
        Book book23 = bookMapper.selectBookById(3);
        Book book24 = bookMapper.selectBookById(4);
        Book book25 = bookMapper.selectBookById(5);
        Book book26= bookMapper.selectBookById(6);
        Book book27 = bookMapper.selectBookById(7);
        Book book28 = bookMapper.selectBookById(8);
        Book book29 = bookMapper.selectBookById(9);
        Book book30 = bookMapper.selectBookById(10);
        Book book31 = bookMapper.selectBookById(11);


    }

}
